﻿//********************************************************************
// 
//  file:  frmUserInformation.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;
using data.diomedestorage.com;

namespace Diomede.Explorer
{
    public partial class frmUserInformation : Form
    {
        public frmUserInformation()
        {
            InitializeComponent();
        }

        public frmUserInformation(ISession currentSession)
        {
            InitializeComponent();
            Session = currentSession;
        }

        private void frmUserInformation_Load(object sender, EventArgs e)
        {
            txtYear.Minimum = DateTime.Today.Year - 2000;
            txtYear.Maximum = txtYear.Minimum + 10;
            Timer tm = new Timer();
            tm.Interval = 500;
            tm.Tick += new EventHandler(tm_Tick);
            tm.Start();
        }

        void tm_Tick(object sender, EventArgs e)
        {
            (sender as Timer).Stop();
            getUserInfo();
        }

        void getUserInfo()
        {
            if (Session != null)
            {
                paneLoading.Visible = true;
                lbltext.Text = "Retrieving user information.";
                paneLoading.Refresh();

                IAccountManagement a = Session as IAccountManagement;
                ISessionExecResult result = a.GetAccountInfo();
                if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                {
                    loadInfo(result.UserInfo);
                }

                paneLoading.Visible = false;
            }
        }

        void loadInfo(UserInfo info)
        {
            //txtEmail.Text = info.email;
            txtCompanyName.Text = info.companyName;
            txtWebsiteUrl.Text = info.websiteUrl;
            txtLastname.Text = info.lastName;
            txtFirstname.Text = info.firstName;
            txtPhone.Text = info.phone;
            txtCardName.Text = info.cardName;
            txtCardNumber.Text = info.cardNumber;
            if (info.cardExpiryMonth >= txtMonth.Minimum && info.cardExpiryMonth <= txtMonth.Maximum)
            {
                txtMonth.Value = info.cardExpiryMonth;
            }
            if (info.cardExpiryYear >= txtYear.Minimum && info.cardExpiryYear <= txtYear.Maximum)
            {
                txtYear.Value = info.cardExpiryYear;
            }
            txtCv2.Text = info.cardCvv2;
            txtAddress1.Text = info.cardAddress1;
            txtAddress2.Text = info.cardAddress2;
            txtCity.Text = info.cardCity;
            txtState.Text = info.cardState;
            txtCountry.Text = info.cardCountry;
            txtZip.Text = info.cardZip;
        }

        bool saveInfo()
        {
            IAccountManagement a = Session as IAccountManagement;
            UserInfo info = new UserInfo
            {
                //email = txtEmail.Text,
                companyName = txtCompanyName.Text,
                websiteUrl = txtWebsiteUrl.Text,
                lastName = txtLastname.Text,
                firstName = txtFirstname.Text,
                phone = txtPhone.Text,
                cardName = txtCardName.Text,
                cardNumber = txtCardNumber.Text,
                cardExpiryMonth = (int)txtMonth.Value,
                cardExpiryYear = (int)txtYear.Value,
                cardCvv2 = txtCv2.Text,
                cardAddress1 = txtAddress1.Text,
                cardAddress2 = txtAddress2.Text,
                cardCity = txtCity.Text,
                cardState = txtState.Text,
                cardCountry = txtCountry.Text,
                cardZip = txtZip.Text
            };

            lbltext.Text = "Saving information...";
            paneLoading.Visible = true;
            paneLoading.Refresh();

            ISessionExecResult result = a.UpdateAccountInfo(info);
            paneLoading.Visible = false;

            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                return true;
            }
            else
            {
                MessageBox.Show(result.Message);
                return false;
            }
        }

        public ISession Session { get; set; }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!saveInfo())
            {
                return;
            }
            MessageBox.Show("Update complete.");

        }

        private void btnDeleteUserInfo_Click(object sender, EventArgs e)
        {
            IAccountManagement a = Session as IAccountManagement;
            ISessionExecResult result = a.DeleteAccountInfo(UserInfoType.allUserInfo | UserInfoType.phone);

            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                getUserInfo();
                MessageBox.Show("User Info Deleted.");
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }

        private void btnDeleteBillingInfo_Click(object sender, EventArgs e)
        {
            IAccountManagement a = Session as IAccountManagement;
            ISessionExecResult result =  a.DeleteAccountInfo(UserInfoType.allBillingInfo);

            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                getUserInfo();
                MessageBox.Show("Billing Info Deleted.");
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }





    }
}
